//
//  GWMyTextView.m
//  新浪微博
//
//  Created by qianfeng on 16/1/15.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWMyTextView.h"
@interface GWMyTextView()
@property (nonatomic,weak) UILabel *label;
@end
@implementation GWMyTextView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        UILabel *label =[[UILabel alloc]init];
         self.label =label;
         [self addSubview:label];
        self.font =[UIFont systemFontOfSize:15];
        self.label.textColor =[UIColor lightGrayColor];
        label.numberOfLines =0;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewChange) name:UITextViewTextDidChangeNotification object:self];
      
        
        self.alwaysBounceVertical =YES;
    }
    return self;
}
-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder =[placeholder copy];
    self.label.text =placeholder;
    [self setNeedsLayout];
}
-(void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor =placeholderColor;
    self.label.textColor =placeholderColor;
    
}
-(void)textViewChange
{
    self.label.hidden =self.attributedText.length!=0;
}
-(void)setText:(NSString *)text
{
    [super setText:text];
    [self textViewChange];
}
-(void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    [self textViewChange];
}
-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.label.font =font;
    [self setNeedsLayout];
}
-(void)layoutSubviews
{
    self.label.frame =CGRectMake(7, 8, self.superview.width-14, [self.label.text boundingRectWithSize:CGSizeMake(self.superview.width-20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size.height);
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
